package com.changgou.collect.service.impl;

import com.changgou.collect.dao.SkuMapper;
import com.changgou.collect.dao.SpuMapper;
import com.changgou.collect.dao.TraceMapper;
import com.changgou.collect.pojo.Trace;
import com.changgou.collect.service.TraceService;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import entity.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TraceServiceImpl implements TraceService {
    @Autowired
    private TraceMapper traceMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private IdWorker idWorker;


    @Override
    public void add(String skuid, String username) {
        Sku sku = skuMapper.selectByPrimaryKey(skuid);

        Trace trace = new Trace();
        trace.setTraceId(idWorker.nextId());
        trace.setUsername(username);
        trace.setName(sku.getName());
        trace.setPrice(sku.getPrice());
        trace.setImage(sku.getImage());
        trace.setSpuid(sku.getSpuId());

        Spu spu = spuMapper.selectByPrimaryKey(sku.getSpuId());
        trace.setSaleService(spu.getSaleService());

        traceMapper.insertSelective(trace);
    }

    @Override
    public List<Trace> findList(String username) {
        Trace trace = new Trace();
        trace.setUsername(username);
        List<Trace> collects = traceMapper.select(trace);
        return collects;
    }
}
